package org.jeecgframework.codegenerate.database;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.jeecgframework.codegenerate.config.DbConfig;

public class CodegenDatasourceConfig {
 private static final Pattern pattern = Pattern.compile("currentSchema=([^&]+)");

 public CodegenDatasourceConfig() {}

 public static void initDbConfig(String divername, String url, String username, String password) {
	 DbConfig.diverName = divername;
	 DbConfig.url = url;
     if (StringUtils.isNotBlank(url) && url.contains("currentSchema")) {
         String schemaName = getSchemaName(url);
         if (StringUtils.isNotBlank(schemaName)) {
        	 DbConfig.schemaName = schemaName;
         }
     }
     DbConfig.username = username;
     DbConfig.password = password;
 }

 private static String getSchemaName(String var0) {
     try {
         Matcher matcher = pattern.matcher(var0);
         return matcher.find() ? matcher.group(1) : null;
     } catch (Exception e) {
         return null;
     }
 }
}
